package org.example.sword2offer.primary;

/**
 * @author: lynn
 * @Descript: 构建乘积数组
 * @date: 2021/3/22 21:09
 * @version: 1.0
 */
public class Q51_MultiplyArray {


    public static void main(String[] args) {
        int[] arr={1,2,3,4,5};
        System.out.println(multiply(arr));
    }
    //给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],
    // 其中B中的元素B[i]=A[0]A[1]...*A[i-1]A[i+1]...*A[n-1]。不能使用除法
    public static int[] multiply(int[] arr) {
        int length=arr.length;
        int temp=1;
        int[] B =new int[length];
        for (int i = 0; i <length ; i++) {
            for (int j = 0; j <length ; j++) {
                if (i==j){
                    continue;
                }
                temp=temp*arr[j];
            }
            B[i]=temp;
            temp=1;
        }
        return B;
    }
}
